import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_OTHER_IN_LOAD_OPS_FORM_DATA,
  SAGA_OTHER_IN_FINISH_OPS,
  SAGA_OTHER_IN_STASH_OPS_DATA,
  SAGA_OTHER_IN_LOAD_ITEM_DETAILS,
  SAGA_OTHER_IN_LOAD_BARCODE_DETAILS,
  SAGA_OTHER_IN_DEL_BARCODE,
  SAGA_OTHER_IN_PUSH_2_SERVER
} from "./saga-types";
import {
  SET_OTHER_IN_OPS_FORM_DATA,
  UPDATE_OTHER_IN_OPS_FORM_DATA,
  SET_OTHER_IN_ITEM_DETAILS,
  SET_OTHER_IN_BARCODE_DETAILS,
  DEL_OTHER_IN_BARCODE
} from "./action-types";

function* loadOtherInOpsFormDataSaga() {
  yield takeEvery(SAGA_OTHER_IN_LOAD_OPS_FORM_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_OTHER_IN_OPS_FORM_DATA]
        },
      ],
    });
  });
}

function* finishOtherInOpsSaga() {
  yield takeEvery(SAGA_OTHER_IN_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* stashOtherInOpsDataSaga() {
  yield takeEvery(SAGA_OTHER_IN_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          respActions: [UPDATE_OTHER_IN_OPS_FORM_DATA],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadOtherInItemDetailsSaga() {
  yield takeEvery(SAGA_OTHER_IN_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInDetailsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_OTHER_IN_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadOtherInBarcodeDetailsSaga() {
  yield takeEvery(SAGA_OTHER_IN_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInDetailsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_OTHER_IN_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* delOtherInBarcodeSaga() {
  yield takeEvery(SAGA_OTHER_IN_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInDetailsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_OTHER_IN_BARCODE],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* pushOtherIn2ServerSaga() {
  yield takeEvery(SAGA_OTHER_IN_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OtherInHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  loadOtherInOpsFormDataSaga(),
  finishOtherInOpsSaga(),
  stashOtherInOpsDataSaga(),
  loadOtherInItemDetailsSaga(),
  loadOtherInBarcodeDetailsSaga(),
  delOtherInBarcodeSaga(),
  pushOtherIn2ServerSaga()
];